using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DealerPlatform.Service.ProductApp;
using DealerPlatform.Service.ShoppingCartApp;
using DealerPlatform.Service.ShoppingCartApp.Dto;
using Microsoft.AspNetCore.Mvc;

namespace DealerPlatform.Web.Controllers
{
    public class ShoppingCartController : AuthenticationController
    {
        public ShoppingCartController(IShoppingCartService service,
            IProductService productService)    //创建总线提供程序的实例
        {
            Service = service;
        }

        public IShoppingCartService Service { get; }

        [HttpPost]
        public async Task<ShoppingCartDto> Insert(ShoppingCartInputDto dto){
            return await Service.SetShoppingCart(dto);
        }

        [HttpGet]
        public async Task<dynamic> GetList(string customerNo)
        {
            var carts = await Service.GetShoppingCartDto(customerNo);
            var types = carts.Select(c=> new {c.ProductDto?.TypeNo, c.ProductDto?.TypeName}).Distinct().ToList();
            return new{carts, types};
        }
    }
}